package com.atguigu.yygh.hosp.controller;

import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author:ahang
 * @create:2022-08-21 0:05
 * @Description:
 */
@Api(description = "医院设置接口")
//@CrossOrigin //跨域
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;

    @GetMapping("/findAll")
    @ApiOperation("医院设置列表")
    public R findAll(){

//        try {
//            int a = 10/0;
//        }catch(Exception e) {
//            throw new YyghException(20001,"出现自定义异常");
//        }
        List<HospitalSet> list = hospitalSetService.list();
        return R.ok().data("list",list);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("医院设置删除")
    public R deleteById(@ApiParam(name = "id" , value = "医院设置编号")
                            @PathVariable("id") Long id){
        hospitalSetService.removeById(id);
        return R.ok();
    }

    @ApiOperation(value = "分页查询")
    @GetMapping("{page}/{limit}")
    public R pageList(@ApiParam(name = "page",value = "第几页")
                        @PathVariable("page") Integer page,
                      @ApiParam(name = "limit",value = "每页显示多少条")
                      @PathVariable("limit") Integer limit){
        //校验参数值
        if(page < 1){
            page = 1;
        }
        if(limit < 0){
            limit = 5;
        }
        //封装分页参数
        Page<HospitalSet> hospitalSetPage = new Page<>(page,limit);
        //service层调用mapper中selectPage方法
        //下面可以不用返回值,只是做引用处理,将查询的结果自动封装进hospitalSetPage中
        hospitalSetService.page(hospitalSetPage);

        //从分页对象中获取分页数据
        List<HospitalSet> records = hospitalSetPage.getRecords();//当前页数据
        long total = hospitalSetPage.getTotal();//总记录数

        return R.ok().data("list",records).data("total",total);
    }

    @ApiOperation(value = "条件分页查询")
    @PostMapping("{page}/{limit}")
    public R pageQuery(@ApiParam(name = "page",value = "第几页")
                        @PathVariable("page") Integer page,
                       @ApiParam(name = "limit",value = "每页显示多少条")
                      @PathVariable("limit") Integer limit,
                       @RequestBody HospitalSetQueryVo hospitalSetQueryVo){
        //校验参数值
        if(page < 1){
            page = 1;
        }
        if(limit < 0){
            limit = 5;
        }
        //封装分页参数
        Page<HospitalSet> hospitalSetPage = new Page<>(page,limit);

        //拼接条件查询(直接使用QueryWrapper接收对象)
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();

        if(hospitalSetQueryVo != null){
            String hoscode = hospitalSetQueryVo.getHoscode();
            String hosname = hospitalSetQueryVo.getHosname();

            if(!StringUtils.isEmpty(hoscode)){
                queryWrapper.eq("hoscode",hoscode);
            }
            if(!StringUtils.isEmpty(hosname)){
                queryWrapper.like("hosname",hosname);
            }
        }

        //service层调用mapper中selectPage方法
        //下面可以不用返回值,只是做引用处理,将查询的结果自动封装进hospitalSetPage中
        hospitalSetService.page(hospitalSetPage,queryWrapper);

        //从分页对象中获取分页数据
        List<HospitalSet> records = hospitalSetPage.getRecords();//当前页数据
        long total = hospitalSetPage.getTotal();//总记录数

        return R.ok().data("list",records).data("total",total);
    }

    @ApiOperation(value = "添加医院设置")
    @PostMapping("saveHospSet")
    public R save(@RequestBody HospitalSet hospitalSet){
        //判断编号是否重复
        boolean duplicate = hospitalSetService.codeIsDuplicate(null,hospitalSet.getHoscode());
        if(duplicate){
            return R.error().message("医院设置编号不能重复");
        }

        //默认状态为0,修改默认值为1
        hospitalSet.setStatus(1);
        hospitalSetService.save(hospitalSet);
        return R.ok();
    }

    @ApiOperation(value = "根据id查询医院设置")
    @GetMapping("getHospSet/{id}")
    public R getById(@PathVariable("id") Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return R.ok().data("item",hospitalSet);
    }

    @ApiOperation(value = "修改医院设置")
    @PutMapping("updateHospSet")
    public R updateById(@RequestBody(required = true) HospitalSet hospitalSet){
        //判断编号是否重复
        boolean duplicate = hospitalSetService.codeIsDuplicate(hospitalSet.getId(),hospitalSet.getHoscode());
        if(duplicate){
            return R.error().message("医院设置编号不能重复");
        }
        //根据id查询的医院信息包括更新时间,会一同返回对象中,所以需要手动的调用修改时间戳
        hospitalSet.setUpdateTime(new Date());
        boolean b = hospitalSetService.updateById(hospitalSet);
        return R.ok().success(b).message(b?"修改成功":"修改失败");
    }

    @ApiOperation(value = "批量删除医院设置")
    @DeleteMapping("batchRemove")
    public R batchRemove(@RequestBody List<Long> ids){
        //判断集合是否为空
        if(ids == null || ids.size() == 0){
            return R.error().message("参数不能为空");
        }
        hospitalSetService.removeByIds(ids);
        return R.ok().message("删除成功");
    }

    @ApiOperation(value = "锁定和解锁")
    @PutMapping("lockHospitalSet/{id}/{status}")
    public R lockHospitalSet(@PathVariable("id") Long id,
                             @PathVariable("status") Integer status){
        //进行参数校验
        if(!StringUtils.isEmpty(id) && id > 0 && (status == 0 || status == 1)){
            HospitalSet hospitalSet = hospitalSetService.getById(id);
            if(hospitalSet == null){
                return R.error().message("医院设置不存在");
            }
            //修改医院状态
            hospitalSet.setStatus(status);
            //更新修改时间
            hospitalSet.setUpdateTime(new Date());
            boolean b = hospitalSetService.updateById(hospitalSet);
            return R.ok().success(b).message("修改成功");
        }
        return R.error().message("请输入正确参数");
    }
}
